<%@ page import="net.sf.ehcache.CacheManager" %>
<%@ page import="net.sf.ehcache.Statistics" %>
<%@ page import="java.util.Arrays" %>
<%@ page import="java.util.Comparator" %>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/common/taglib.jsp" %>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="renderer" content="webkit">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta content="微社区,Q生活" name="keywords"/>
<meta content="微社区,Q生活" name="description"/>
<title>微社区|Q生活</title>
<link rel="stylesheet" href="${ctx }/static/css/Master.css"/>

<script src="${ctx }/static/js/jquery-1.9.0.min.js"></script>
<script src="${ctx}/static/plugins/layer/layer.js"></script>
<%-- 基础js,所有页面都需要引入此js,依赖layer.js --%>
<script src="${ctx}/static/js/common/base.js"></script>
<script src="${ctx}/static/js/common/util.js"></script>
</head>

<body class="utilSubmit">
<section class="boxRight">
<form id="searchForm" method="get" >
<div class="context boxborder">

	<!-- 顶部按钮 -->
	<div class="pr prlink">
		<a href="?">默认排序</a>
		<a href="?sort=hitCount">总命中率排序</a>
		<a href="?sort=objectCount">总对象数排序</a>
	</div>
	
	
	<!-- 内容 -->
	<div class="com_table">
		<table width="100%" cellpadding="0" cellspacing="0">
			<tr>
				<th>序号</th>
				<th>缓存名称</th>
				<th>总命中率</th>
				<th>命中次数</th>
				<th>失效次数</th>
				<th>总对象数</th>
				<th>最后一秒查询完成的执行数</th>
				<th>最后一次采样的平均执行时间(毫秒)</th>
				<th>平均获取时间(毫秒)</th>
				<th>操作</th>
			</tr>
			<%
	            CacheManager cacheManager = (CacheManager) request.getAttribute("cacheManager");
	            String[] cacheNames = cacheManager.getCacheNames();
	            int length = cacheNames.length;
	
	            sort(cacheNames, cacheManager, request.getParameter("sort"));
	
	            for (int i = 0; i < length; i++) {
	                String cacheName = cacheNames[i];
	                pageContext.setAttribute("index",i+1);
	                pageContext.setAttribute("cacheName", cacheName);
	                pageContext.setAttribute("cache", cacheManager.getCache(cacheName));
	        %>
			        <c:set var="totalCount"
				               value="${cache.statistics.cacheHits + cache.statistics.cacheMisses}"/>
			        <c:set var="totalCount" value="${totalCount > 0 ? totalCount : 1}"/>
			        <c:set var="cacheHitPercent" value="${cache.statistics.cacheHits * 1.0 / (totalCount)}"/>
					<tr>
						<td>${index}</td>
						<td>${cacheName}</td>
						<td style="text-align: right;">
							<div class="utilTips">
								<fmt:formatNumber type="number" value="${cacheHitPercent}" pattern="0.000000"/>
		                		<div class="utilTipsText" style="display: none;">${cacheHitPercent}</div>
							</div>
						</td>
		                <td style="text-align: right;">${cache.statistics.cacheHits}</td>
						<td style="text-align: right;">${cache.statistics.cacheMisses}</td>
						<td style="text-align: right;">${cache.statistics.objectCount}</td>
						<td style="text-align: right;">${cache.statistics.searchesPerSecond}</td>
						<td style="text-align: right;">${cache.statistics.averageSearchTime}</td>
						<td style="text-align: right;">
							<div class="utilTips">
								<fmt:formatNumber type="number" value="${cache.statistics.averageGetTime}" pattern="0.000000"/>
		                		<div class="utilTipsText" style="display: none;">${cache.statistics.averageGetTime}</div>
							</div>
						</td>
						<td>
							<a class="icon micon_9" title="详情" href="${ctx}/monitor/ehcache/${cacheName}/detailList"></a>
						</td>
					</tr>
			<%
	            }
	        %>
		</table>
		
	</div>
</div>
</form>
</section>
</body>
</html>

<!-- 分割线 -->

<%!
        private void sort(final String[] cacheNames, final CacheManager cacheManager, final String sort) {
            Arrays.sort(cacheNames, new Comparator<String>() {
                public int compare(String n1, String n2) {
                    Statistics s1 = cacheManager.getCache(n1).getStatistics();
                    Statistics s2 = cacheManager.getCache(n2).getStatistics();
                    if("hitCount".equals(sort)) {
                        double n1HitPercent = 1.0 * s1.getCacheHits() / Math.max(s1.getCacheHits() + s1.getCacheMisses(), 1);
                        double n2HitPercent = 1.0 * s2.getCacheHits() / Math.max(s2.getCacheHits() + s2.getCacheMisses(), 1);
                        return -Double.compare(n1HitPercent, n2HitPercent);
                    } else if("objectCount".equals(sort)) {
                        return -Long.valueOf(s1.getObjectCount()).compareTo(Long.valueOf(s2.getObjectCount()));
                    }
                    return -n1.compareTo(n2);
                }

            });
        }
%>